Repository navigation
fix(shell): stop stdin_write from blocking the main thread - #3782
AllahuRodrigues wants to merge 1 commit into
Conversation
stdin_write is a sync command and did a blocking write_all while holding the children lock, so a child that wasn't reading stdin froze the window. It now queues the data for a per-child writer thread; the command stays sync so writes keep their order.
Package Changes Through ba5d898There are 2 changes which include shell with patch, shell-js with patch Planned Package VersionsThe following package releases are the planned based on the context of changes in this pull request.
Add another change file through the GitHub UI by following this link. Read about change files or the docs at github.com/jbolda/covector |
i wonder if it makes sense or is possible to combine your queue with an async command so that you keep the order but also the background thread queue, or maybe that still adds the risk of out of order messsages 🤔 |
|
Yeah, I considered that too. My main concern is actually where the writes enter the queue. The queue itself preserves order, but if we make That's why I kept the command sync. It only copies the bytes into the queue and returns. The actual blocking I/O happens on the writer thread. So we're still moving the expensive part off the main thread, without introducing another ordering problem. I think we could combine both approaches, but we'd need to guarantee ordering before the async tasks start enqueueing. Another option would be to keep the sync enqueue and give each write an ID, then report its result once the background thread finishes. That would let us associate errors with the correct write, rather than reporting them on the next call. It does add a bit more complexity though, so I didn't want to introduce that unless we actually need it. Would you prefer that approach, or do you have another way in mind to preserve the call order with async commands? |
Fixes #3748.
Hey! So I looked into the issue where calling
stdin_writecan freeze the application window when the child process isn't reading from stdin.The problem seems to be that
stdin_writeis currently synchronous and callswrite_alldirectly while holding the children lock.So if the child process stops reading or takes a while to read, the write blocks once the pipe buffer fills up, and the whole window can freeze until the child starts reading again.
What I changed
Initially, I considered making the command async, similar to the
killchange in #3351.But there's a potential issue with that approach. If JavaScript calls
child.write()multiple times without awaiting each one, the writes could execute on different threads and potentially arrive out of order.So instead, I kept the command synchronous but changed what it actually does.
Now, rather than writing directly to stdin, it copies the data into a queue for that child process.
A background writer thread then takes care of writing everything to stdin in the correct order.
The children lock is only held briefly while adding data to the queue, so the actual write no longer blocks the window.
A couple of things worth mentioning
Since the actual write happens in the background, errors such as the child process exiting are reported on the next
write()call.Also, when the child is dropped, the queued data is drained before stdin closes.
Testing
I added two tests.
write_queued_does_not_wait_for_reader_and_keeps_orderThis writes 1 MiB of data followed by a short string to:
The child waits one second before reading anything.
With the new implementation, both writes return immediately, and the output matches the input byte for byte.
I also ran this against the old implementation to make sure the test actually catches the original issue. It failed with:
So the regression test does catch the blocking behavior.
write_queued_reports_error_after_child_exitsThis checks that writing to a child process that has already exited correctly reports an error.
Final checks
-D warnings.The new tests are under
cfg(not(windows)), consistent with the other spawn tests in the file.One limitation is that the queue is currently unbounded, so an application writing large amounts of data to a child that never reads could increase memory usage.
I considered adding a bounded queue, but wanted to keep this fix focused on the original blocking issue first.
Also, the public Rust
CommandChild::writeAPI is unchanged and still blocking. This only changes the JavaScript command path.Let me know what you think, especially if you'd prefer a bounded queue or a different approach to handling write errors!